import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 习题类 - 实验内容1
 * 设计习题类中的成员变量和成员函数
 */
public class Exercise {
    private List<Expression> expressions;
    private Random random;
    
    public Exercise() {
        expressions = new ArrayList<>();
        random = new Random();
    }
    
    public void addExpression(Expression expr) {
        expressions.add(expr);
    }
    
    public void generateExercises(int count) {
        for (int i = 0; i < count; i++) {
            int left = random.nextInt(100) + 1;
            int right = random.nextInt(100) + 1;
            String operator = random.nextBoolean() ? "+" : "-";
            expressions.add(new Expression(left, right, operator));
        }
    }
    
    public void displayQuestions() {
        System.out.println("========== 习题集 ==========");
        for (int i = 0; i < expressions.size(); i++) {
            System.out.println((i + 1) + ". " + expressions.get(i).getExpression());
        }
        System.out.println("============================");
    }
    
    public void displayAnswers() {
        System.out.println("========== 参考答案 ==========");
        for (int i = 0; i < expressions.size(); i++) {
            System.out.println((i + 1) + ". " + expressions.get(i).toString());
        }
        System.out.println("============================");
    }
    
    public int getSize() {
        return expressions.size();
    }
    
    public String getStatistics() {
        long addCount = expressions.stream()
            .filter(expr -> "+".equals(expr.getOperator()))
            .count();
        long subCount = expressions.size() - addCount;
        
        return String.format("总题数: %d, 加法: %d, 减法: %d", expressions.size(), addCount, subCount);
    }
}